﻿using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace backend.Entity
{
    public class MyContext : DbContext
    {
        public MyContext()
        {
        }

        public MyContext(DbContextOptions<MyContext> options)
               : base(options)
        {
        }

        public DbSet<User> Users { get; set; }
        public DbSet<Project> Projects { get; set; }
        public DbSet<Payment> Payments { get; set; }
        public DbSet<Parametric> Parametrics { get; set; }
        public DbSet<Report> Reports { get; set; }
        public DbSet<Attachment> Attachments { get; set; }
        public DbSet<FileInfo> FileInfos { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Project>(b =>
            {
                b.HasMany(x => x.Users).WithMany(a => a.Projects)
                .UsingEntity<Dictionary<string, object>>(
                    "Project_User",
                    j => j.HasOne<User>().WithMany().HasForeignKey("UserName").OnDelete(DeleteBehavior.Cascade),
                    j => j.HasOne<Project>().WithMany().HasForeignKey("ProjectId").OnDelete(DeleteBehavior.ClientCascade)
                );
            });
        }
    }
}